package stack_queue.leetcode;

import java.util.Stack;

/**
 * 双栈实现最小栈
 * https://leetcode-cn.com/problems/min-stack/
 */
public class MinStack {
    // 具体存储元素
    private Stack<Integer> element = new Stack<>();
    // 辅助栈，一直保存最小元素
    private Stack<Integer> support = new Stack<>();

    public MinStack() {
    }

    public void push(int val) {
        element.push(val);
        if (support.isEmpty()) {
            support.push(val);
        }else {
            int tmpMin = support.peek();
            // 比较当前元素和tmpMin谁更小
            // 将最小的元素push进辅助栈中
            int min = Math.min(val,tmpMin);
            support.push(min);
        }
    }

    public void pop() {
        element.pop();
        support.pop();
    }

    public int top() {
        return element.peek();
    }

    public int getMin() {
        return support.peek();
    }
}
